//
// Created by yonghu0 on 25-10-19.
//
#include <jsoncpp/json/json.h>
#include <iostream>
#include "HttpServer.h"
#include "../include/httplib.h"
#include "Control.h"
#include "Logger.h"

Control control;

// 获取请求中的Token
std::string GetRequestToken(const httplib::Request &req)
{
    auto res = req.headers.find("Authorization");

    if (res != req.headers.end())
        return res->second;
    else
        return "0";
}

bool SetResponseStatus(const Json::Value &resjson, httplib::Response &rsp)
{
    std::string result = resjson["Result"].asString();
    if (result == "Success")
    {
        return true;
    }
    else if (result == "Fail")
    {
        return true;
    }
    else if (result == "400") //请求参数有误
    {
        rsp.status = 400;
    }
    else if (result == "401") //无权限
    {
        rsp.status = 401;
    }
    else if (result == "500") //服务器错误
    {
        rsp.status = 500;
    }
    return true;
}

void DoRegisterUser(const httplib::Request &req, httplib::Response &rsp)
{
    LOG_INFO("DoRegisterUser begin");
    Json::Reader reader;
    Json::Value value;
    reader.parse(req.body, value);

    Json::Value resjson = control.RegisterUser(value);

    SetResponseStatus(resjson, rsp);
    rsp.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
    LOG_INFO("DoRegisterUser end");
}

void DoLoginUser(const httplib::Request &req, httplib::Response &rsp)
{
    LOG_INFO("DoLoginUser begin");
    Json::Reader reader;
    Json::Value value;
    reader.parse(req.body, value);

    Json::Value resjson = control.LoginUser(value);

    SetResponseStatus(resjson, rsp);
    rsp.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
    LOG_INFO("DoLoginUser end");
}

void DoSelectUserInfo(const httplib::Request &req, httplib::Response &rsp)
{
    LOG_INFO("DoSelectUserInfo begin");
    Json::Value resjson;
    if (req.has_param("UserId"))
    {
        Json::Value value;
        std::string userid = req.get_param_value("UserId");
        value["UserId"] = userid;
        value["Token"] = GetRequestToken(req);

        resjson = control.SelectUserInfo(value);
    }
    else
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误";
    }

    SetResponseStatus(resjson, rsp);
    rsp.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
    LOG_INFO("DoSelectUserInfo end");
}

void DoUpdateUser(const httplib::Request &req, httplib::Response &rsp)
{
    LOG_INFO("DoUpdateUser begin");
    Json::Reader reader;
    Json::Value value;
    reader.parse(req.body, value);
    value["Token"] = GetRequestToken(req);

    Json::Value resjson = control.UpdateUser(value);

    SetResponseStatus(resjson, rsp);
    rsp.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
    LOG_INFO("DoUpdateUser end");
}

void DoGetUserUpdateInfo(const httplib::Request &req, httplib::Response &rsp)
{
    LOG_INFO("DoGetUserUpdateInfo begin");
    Json::Value resjson;
    if (req.has_param("UserId"))
    {
        Json::Value value;
        std::string userid = req.get_param_value("UserId");
        value["UserId"] = userid;
        value["Token"] = GetRequestToken(req);

        resjson = control.GetUserUpdateInfo(value);
    }
    else
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误";
    }

    SetResponseStatus(resjson, rsp);
    rsp.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
    LOG_INFO("DoGetUserUpdateInfo end");
}

void DoGetUserlistInfo(const httplib::Request &req, httplib::Response &rsp)
{
    LOG_INFO("DoGetUserlistInfo begin");
    Json::Value resjson;
    if (req.has_param("Page") && req.has_param("PageSize"))
    {
        Json::Value value;
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");
        value["Page"] = page;
        value["PageSize"] = pagesize;
        value["Token"] = GetRequestToken(req);

        resjson = control.GetUserlistInfo(value);
    }
    else
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误";
    }

    SetResponseStatus(resjson, rsp);
    rsp.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
    LOG_INFO("DoGetUserlistInfo end");
}

void DoDeleteUser(const httplib::Request &req, httplib::Response &rsp)
{
    LOG_INFO("DoDeleteUser begin");
    Json::Value resjson;
    if (req.has_param("UserId"))
    {
        Json::Value value;
        std::string userid = req.get_param_value("UserId");
        value["UserId"] = userid;
        value["Token"] = GetRequestToken(req);

        resjson = control.DeleteUser(value);
    }
    else
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误";
    }

    SetResponseStatus(resjson, rsp);
    rsp.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
    LOG_INFO("DoDeleteUser end");
}

void DoRankUser(const httplib::Request &req, httplib::Response &rsp)
{
    LOG_INFO("DoRankUser begin");
    Json::Value resjson;
    if (req.has_param("Page") && req.has_param("PageSize"))
    {
        Json::Value value;
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");
        value["Page"] = page;
        value["PageSize"] = pagesize;
        value["Token"] = GetRequestToken(req);

        resjson = control.RankUser(value);
    }
    else
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误";
    }

    SetResponseStatus(resjson, rsp);
    rsp.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
    LOG_INFO("DoRankUser end");
}

void DoGetUserInfoByToken(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetUserInfoByToken start!!!");
    Json::Value queryjson;

    queryjson["Token"] = GetRequestToken(req);
    Json::Value resjson = control.TokenLoginUser(queryjson);

    LOG_INFO("doGetUserInfoByToken end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetProblem(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetProblem start!!!");
    Json::Value resjson;
    if (!req.has_param("ProblemId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string problemid = req.get_param_value("ProblemId");
        Json::Value queryjson;
        queryjson["ProblemId"] = problemid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectProblem(queryjson);
    }
    LOG_INFO("doGetProblem end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetProblemList(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetProblemList start!!!");

    Json::Value resjson;
    if (!req.has_param("SearchInfo") || !req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误！";
    }
    else
    {
        Json::Value serachinfo;
        Json::Reader reader;
        // 解析传入的json
        reader.parse(req.get_param_value("SearchInfo"), serachinfo);
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");

        Json::Value queryjson;
        queryjson["SearchInfo"] = serachinfo;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectProblemList(queryjson);
    }
    LOG_INFO("doGetProblemList end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetProblemListByAdmin(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetProblemListByAdmin start!!!");
    Json::Value resjson;
    if (!req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误！";
    }
    else
    {
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");
        Json::Value queryjson;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectProblemListByAdmin(queryjson);
    }
    LOG_INFO("doGetProblemListByAdmin end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetProblemInfo(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetProblemInfo start!!!");
    Json::Value resjson;

    if (!req.has_param("ProblemId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误！";
    }
    else
    {
        std::string problemid = req.get_param_value("ProblemId");

        Json::Value queryjson;
        queryjson["ProblemId"] = problemid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectProblemInfoByAdmin(queryjson);
    }
    LOG_INFO("doGetProblemInfo end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoEditProblem(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doEditProblem start!!!");
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["datainfo"]["Token"] = GetRequestToken(req);
    Json::Value resjson = control.EditProblem(jsonvalue["datainfo"]);

    LOG_INFO("doEditProblem end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoDeleteProblem(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doDeleteProblem start!!!");
    Json::Value resjson;
    if (!req.has_param("ProblemId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误！";
    }
    else
    {
        std::string problemid = req.get_param_value("ProblemId");

        Json::Value deletejson;
        deletejson["ProblemId"] = problemid;
        deletejson["Token"] = GetRequestToken(req);

        resjson = control.DeleteProblem(deletejson);
    }

    LOG_INFO("doDeleteProblem end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetTags(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetTags start!!!");
    Json::Value resjson;
    if (!req.has_param("TagType"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "请求参数有误！";
    }
    else
    {
        Json::Value queryjson;
        std::string tagtype = req.get_param_value("TagType");
        queryjson["TagType"] = tagtype;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.GetTags(queryjson);
    }

    LOG_INFO("doGetTags end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetAnnouncementList(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetAnnouncementList start!!!");

    Json::Value resjson;
    if (!req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");

        Json::Value queryjson;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectAnnouncementList(queryjson);
    }

    LOG_INFO("doGetAnnouncementList end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetAnnouncementListByAdmin(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetAnnouncementListByAdmin start!!!");

    Json::Value resjson;
    if (!req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");

        Json::Value queryjson;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectAnnouncementListByAdmin(queryjson);
    }

    LOG_INFO("doGetAnnouncementListByAdmin end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetAnnouncement(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetAnnouncement start!!!");
    Json::Value resjson;

    if (!req.has_param("AnnouncementId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string announcementid = req.get_param_value("AnnouncementId");
        Json::Value queryjson;
        queryjson["AnnouncementId"] = announcementid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectAnnouncement(queryjson);
    }

    LOG_INFO("doGetAnnouncement end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoSelectAnnouncement(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doSelectAnnouncement start!!!");

    Json::Value resjson;
    if (!req.has_param("AnnouncementId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string announcementid = req.get_param_value("AnnouncementId");

        Json::Value queryjson;
        queryjson["AnnouncementId"] = announcementid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectAnnouncementByEdit(queryjson);
    }
    LOG_INFO("doSelectAnnouncement end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoInsertAnnouncement(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doInsertAnnouncement start!!!");
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["Token"] = GetRequestToken(req);
    Json::Value resjson = control.InsertAnnouncement(jsonvalue);

    LOG_INFO("doInsertAnnouncement end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoUpdateAnnouncement(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doUpdateAnnouncement start!!!");
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["Token"] = GetRequestToken(req);
    Json::Value resjson = control.UpdateAnnouncement(jsonvalue);

    LOG_INFO("doUpdateAnnouncement end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoDeleteAnnouncement(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doDeleteAnnouncement start!!!");
    Json::Value resjson;

    if (!req.has_param("AnnouncementId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string announcementid = req.get_param_value("AnnouncementId");
        Json::Value deletejson;
        deletejson["AnnouncementId"] = announcementid;
        deletejson["Token"] = GetRequestToken(req);

        resjson = control.DeleteAnnouncement(deletejson);
    }

    LOG_INFO("doDeleteAnnouncement end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetDiscussList(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetDiscussList start!!!");
    Json::Value resjson;
    if (!req.has_param("SearchInfo") || !req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        Json::Value searchinfo;
        Json::Reader reader;
        // 解析传入的json
        reader.parse(req.get_param_value("SearchInfo"), searchinfo);
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");
        Json::Value queryjson;
        queryjson["SearchInfo"] = searchinfo;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectDiscussList(queryjson);
    }

    LOG_INFO("doGetDiscussList end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetDiscussListByAdmin(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetDiscussListByAdmin start!!!");
    Json::Value resjson;
    if (!req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");
        Json::Value queryjson;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectDiscussListByAdmin(queryjson);
    }
    LOG_INFO("doGetDiscussListByAdmin end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetDiscuss(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetDiscuss start!!!");
    Json::Value resjson;
    if (!req.has_param("DiscussId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string discussid = req.get_param_value("DiscussId");
        Json::Value queryjson;
        queryjson["DiscussId"] = discussid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectDiscuss(queryjson);
    }

    LOG_INFO("doGetDiscuss end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoSelectDiscussByEdit(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doSelectDiscussByEdit start!!!");
    Json::Value resjson;
    if (!req.has_param("DiscussId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string discussid = req.get_param_value("DiscussId");
        Json::Value queryjson;
        queryjson["DiscussId"] = discussid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectDiscussByEdit(queryjson);
    }
    LOG_INFO("doSelectDiscussByEdit end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoInsertDiscuss(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doInsertSolution start!!!");
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["Token"] = GetRequestToken(req);
    Json::Value resjson = control.InsertDiscuss(jsonvalue);

    LOG_INFO("doInsertSolution end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoUpdateDiscuss(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doUpdateSolution start!!!");
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["Token"] = GetRequestToken(req);
    Json::Value resjson = control.UpdateDiscuss(jsonvalue);

    LOG_INFO("doUpdateSolution end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoDeleteDiscuss(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doDeleteDiscuss start!!!");
    Json::Value resjson;

    if (!req.has_param("DiscussId") || !req.has_param("UserId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string discussid = req.get_param_value("DiscussId");
        std::string userid = req.get_param_value("UserId");

        Json::Value deletejson;
        deletejson["DiscussId"] = discussid;
        deletejson["UserId"] = userid;
        deletejson["Token"] = GetRequestToken(req);

        resjson = control.DeleteDiscuss(deletejson);
    }

    LOG_INFO("doDeleteDiscuss end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetSolutionList(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetSolutionList start!!!");
    Json::Value resjson;

    if (!req.has_param("SearchInfo") || !req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        Json::Value searchinfo;
        Json::Reader reader;
        // 解析传入的json
        reader.parse(req.get_param_value("SearchInfo"), searchinfo);
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");
        Json::Value queryjson;
        queryjson["SearchInfo"] = searchinfo;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectSolutionList(queryjson);
    }
    LOG_INFO("doGetSolutionList end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetSolutionListByAdmin(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetSolutionListByAdmin start!!!");
    Json::Value resjson;
    if (!req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");

        Json::Value queryjson;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectSolutionListByAdmin(queryjson);
    }

    LOG_INFO("doGetSolutionListByAdmin end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetSolution(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetSolution start!!!");
    Json::Value resjson;

    if (!req.has_param("SolutionId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string solutionid = req.get_param_value("SolutionId");
        Json::Value queryjson;
        queryjson["SolutionId"] = solutionid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectSolution(queryjson);
    }

    LOG_INFO("doGetSolution end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoSelectSolutionByEdit(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doSelectSolutionByEdit start!!!");
    Json::Value resjson;

    if (!req.has_param("SolutionId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string solutionid = req.get_param_value("SolutionId");
        Json::Value queryjson;
        queryjson["SolutionId"] = solutionid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectSolutionByEdit(queryjson);
    }

    LOG_INFO("doSelectSolutionByEdit end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoInsertSolution(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doInsertSolution start!!!");
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["Token"] = GetRequestToken(req);
    Json::Value resjson = control.InsertSolution(jsonvalue);

    LOG_INFO("doInsertSolution end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoUpdateSolution(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doUpdateSolution start!!!");
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["Token"] = GetRequestToken(req);
    Json::Value resjson = control.UpdateSolution(jsonvalue);

    LOG_INFO("doUpdateSolution end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoDeleteSolution(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doDeleteSolution start!!!");
    Json::Value resjson;

    if (!req.has_param("SolutionId") || !req.has_param("UserId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string solutionid = req.get_param_value("SolutionId");
        std::string userid = req.get_param_value("UserId");

        Json::Value deletejson;
        deletejson["SolutionId"] = solutionid;
        deletejson["UserId"] = userid;
        deletejson["Token"] = GetRequestToken(req);

        resjson = control.DeleteSolution(deletejson);
    }
    LOG_INFO("doDeleteSolution end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoSelectCommentListByAdmin(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doSelectCommentListByAdmin start!!!");
    Json::Value resjson;

    if (!req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");

        Json::Value queryjson;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectCommentListByAdmin(queryjson);
    }

    LOG_INFO("doSelectCommentListByAdmin end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetComment(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetComment start!!!");
    Json::Value resjson;

    if (!req.has_param("Type") || !req.has_param("ParentId") || !req.has_param("Page") || !req.has_param("PageSize") ||
        !req.has_param("SonNum"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string type = req.get_param_value("Type");
        std::string parentid = req.get_param_value("ParentId");
        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");
        std::string sonsnum = req.get_param_value("SonNum");
        Json::Value queryjson;
        queryjson["Type"] = type;
        queryjson["ParentId"] = parentid;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["SonNum"] = sonsnum;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.GetComment(queryjson);
    }

    LOG_INFO("doGetComment end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoInsertComment(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doInsertComment start!!!");
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["Info"]["Token"] = GetRequestToken(req);
    Json::Value resjson = control.InsertComment(jsonvalue["Info"]);

    LOG_INFO("doInsertComment end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoDeleteComment(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doDeleteComment start!!!");
    Json::Value resjson;
    if (!req.has_param("ArticleId") || !req.has_param("CommentId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string articleid = req.get_param_value("ArticleId");
        std::string commentid = req.get_param_value("CommentId");
        Json::Value deletejson;
        deletejson["ArticleId"] = articleid;
        deletejson["CommentId"] = commentid;
        deletejson["Token"] = GetRequestToken(req);

        resjson = control.DeleteComment(deletejson);
    }

    LOG_INFO("doDeleteComment end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoPostCode(const httplib::Request &req, httplib::Response &res)
{
    Json::Value jsonvalue;
    Json::Reader reader;
    // 解析传入的json
    reader.parse(req.body, jsonvalue);
    jsonvalue["Token"] = GetRequestToken(req);
    Json::Value resjson = control.GetJudgeCode(jsonvalue);

    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetStatusRecordList(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetStatusRecordList start!!!");
    Json::Value resjson;

    if (!req.has_param("SearchInfo") || !req.has_param("Page") || !req.has_param("PageSize"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        Json::Value searchinfo;
        Json::Reader reader;
        // 解析传入的json
        reader.parse(req.get_param_value("SearchInfo"), searchinfo);

        std::string page = req.get_param_value("Page");
        std::string pagesize = req.get_param_value("PageSize");

        Json::Value queryjson;
        queryjson["SearchInfo"] = searchinfo;
        queryjson["Page"] = page;
        queryjson["PageSize"] = pagesize;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectStatusRecordList(queryjson);
    }

    LOG_INFO("doGetStatusRecordList end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetStatusRecord(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetStatusRecordInfo start!!!");

    Json::Value resjson;
    if (!req.has_param("SubmitId"))
    {
        resjson["Result"] = "400";
        resjson["Reason"] = "缺少请求参数！";
    }
    else
    {
        std::string submitid = req.get_param_value("SubmitId");

        Json::Value queryjson;
        queryjson["SubmitId"] = submitid;
        queryjson["Token"] = GetRequestToken(req);

        resjson = control.SelectStatusRecord(queryjson);
    }

    LOG_INFO("doGetStatusRecordInfo end!!!");
    SetResponseStatus(resjson, res);
    res.set_content(resjson.toStyledString(), "application/json;charset=utf-8");
}

void DoGetImage(const httplib::Request &req, httplib::Response &res)
{
    LOG_INFO("doGetImage start!!!");
    int index = stoi(req.matches[1]);
    std::string path = "../WWW/image/avatar" + std::to_string(index) + ".webp";
    std::ifstream infile;
    infile.open(path.data());
    if (!infile.is_open())
    {
        std::string str = "图片获取失败";
        res.set_content(str, "text");
    }
    std::string image((std::istreambuf_iterator<char>(infile)),
                      (std::istreambuf_iterator<char>()));

    LOG_INFO("doGetImage end!!!");
    res.set_content(image, "webp");
}

void HttpServer::Run(unsigned int port)
{
    httplib::Server server;

    // -----------------用户----------------
    //注册用户
    server.Post("/user/register", DoRegisterUser);
    //登录用户
    server.Post("/user/login", DoLoginUser);
    //使用Token登录用户
    server.Get("/user/tokenlogin", DoGetUserInfoByToken);
    //返回用户信息，用于个人主页展示
    server.Get("/user/home", DoSelectUserInfo);
    //更新用户个人主页信息
    server.Post("/user/update", DoUpdateUser);
    //返回用户个人主页信息，用于编辑修改
    server.Get("/user/updateinfo", DoGetUserUpdateInfo);
    //管理员查看用户列表
    server.Get("/userlist/admin", DoGetUserlistInfo);
    //管理员删除用户
    server.Delete("/user", DoDeleteUser);
    //用户排名
    server.Get("/user/rank", DoRankUser);

    // -----------------题目----------------
    //获取题目信息
    server.Get("/problem", DoGetProblem);
    //获取题目列表
    server.Get("/problemlist", DoGetProblemList);
    //管理员获取题目列表
    server.Get("/problemlist/admin", DoGetProblemListByAdmin);
    //管理员获取题目信息，用于编辑修改
    server.Get("/problem/select", DoGetProblemInfo);
    //管理员编辑题目(插入、删除)
    server.Post("/problem/edit", DoEditProblem);
    //管理员删除题目
    server.Delete("/problem", DoDeleteProblem);
    //获取题目标签
    server.Get("/tags", DoGetTags);

    // -----------------公告----------------
    //获取公告列表
    server.Get("/announcementlist", DoGetAnnouncementList);
    //管理员获取公告列表
    server.Get("/announcementlist/admin", DoGetAnnouncementListByAdmin);
    //获取公告信息
    server.Get("/announcement", DoGetAnnouncement);
    //管理员获取公告信息，用于编辑修改
    server.Get("/announcement/select", DoSelectAnnouncement);
    //管理员插入公告
    server.Post("/announcement/insert", DoInsertAnnouncement);
    //管理员更新公告
    server.Post("/announcement/update", DoUpdateAnnouncement);
    //管理员删除公告
    server.Delete("/announcement", DoDeleteAnnouncement);

    // -----------------讨论----------------
    //获取讨论列表
    server.Get("/discusslist", DoGetDiscussList);
    //管理员获取讨论列表
    server.Get("/discusslist/admin", DoGetDiscussListByAdmin);
    //获取讨论信息
    server.Get("/discuss", DoGetDiscuss);
    //获取讨论信息，用于编辑
    server.Get("/discuss/select", DoSelectDiscussByEdit);
    //插入讨论
    server.Post("/discuss/insert", DoInsertDiscuss);
    //更新讨论
    server.Post("/discuss/update", DoUpdateDiscuss);
    //删除讨论
    server.Delete("/discuss", DoDeleteDiscuss);

    // -----------------题解----------------
    //获取题解列表
    server.Get("/solutionlist", DoGetSolutionList);
    //管理员获取题解列表
    server.Get("/solutionlist/admin", DoGetSolutionListByAdmin);
    //获取题解信息
    server.Get("/solution", DoGetSolution);
    //获取题解信息用于编辑
    server.Get("/solution/select", DoSelectSolutionByEdit);
    //插入题解
    server.Post("/solution/insert", DoInsertSolution);
    //更新题解
    server.Post("/solution/update", DoUpdateSolution);
    //删除题解
    server.Delete("/solution", DoDeleteSolution);

    // -----------------评论----------------
    //管理员获取评论列表
    server.Get("/commentlist/admin", DoSelectCommentListByAdmin);
    //获取评论内容
    server.Get("/comment", DoGetComment);
    //插入评论
    server.Post("/comment/insert", DoInsertComment);
    //删除评论
    server.Delete("/comment", DoDeleteComment);

    // -----------------提交状态----------------
    //提交代码
    server.Post("/problemcode", DoPostCode);
    //获取提交状态列表
    server.Get("/statusrecordlist", DoGetStatusRecordList);
    //获取提交状态内容
    server.Get("/statusrecord", DoGetStatusRecord);

    //获取个人主页头像
    server.Get(R"(/image/(\d+))", DoGetImage);

    server.set_base_dir("../WWW");

    server.listen("0.0.0.0", port);
}
